//
//  YZMStockBuyCheckViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockBuyCheckViewController.h"
#import "YZClient.h"
#import "MBProgressHUD+Utils.h"
#import "YZMConfigureService.h"
#import "YZMWebViewService.h"
#import <BlocksKit/UIControl+BlocksKit.h>

@interface YZMStockBuyCheckViewController()

@property (nonatomic, strong) UIButton *submitBT;

@property (nonatomic, assign) NSInteger tradeType;

@end

@implementation YZMStockBuyCheckViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.title = @"签署协议";
    
    [self createObjects];
}

- (void)updateWithParams: (NSDictionary *)params
{
    if ([params.allKeys containsObject:@"tradeType"]) {
        self.tradeType = [params[@"tradeType"] intValue];
    }
}

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMStockBuyCheckViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

#pragma mark -- network

- (void)submitDelegateAgreeRequest:(UIButton *)sender{
    
    MBProgressHUD *hud = [MBProgressHUD showMessageManual:@"签署中"];
    WS(ws);
    [YZClient td_signTradeAgreementSuccess:^(NSURLResponse *response, id responseObject) {
        
        [hud hide:NO];
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [YZMConfigureService sharedService].defaultConfigure.signAgreement = YES;
            if (ws.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB) {
                [YZMYLBConfigureService sharedService].defaultConfigure.signAgreement = YES;
            }
            [YZMHCMInstance popViewController:nil animated:YES];
            
        }else{
            
            [topModel showMessage];
        }
        
    } failure:^(NSError *error) {
       
        [hud hide:NO];
        [error yz_showMessage];
        
    }];
}

#pragma mark -- event

- (void)delegatePressed:(UIButton *)sender{
    
    switch (sender.tag) {
        case 0:
            [YZMWebViewService openFeeStandard:self.tradeType];
            break;
        case 1:{
            if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB) {
                [YZMWebViewService openRegisterAgreement];
            }else{
                [YZMWebViewService openCooperationDelegate];
            }
        }
            break;
        case 2:
            [YZMWebViewService openRegisterAgreement];
            break;
        default:
            break;
    }
}

#pragma mark -- ui

- (void)createObjects{
    
    UILabel *label = [YZMAppStyle labelWithSmallLightGray];
    label.text = @"重要提示：\n请您务必在参与微策略实盘交易前详细阅读本规则，并明白微策略交易的风险性。";
    label.numberOfLines = 0;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(self.view).offset(20);
    }];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 6;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text attributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    label.attributedText = attributedString;
    
    UIButton *button = [UIButton new];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.selected = YES;
    [button setImage:[UIImage imageNamed:@"icon_choose_button"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"icon_choose_button_selected"] forState:UIControlStateSelected];
    [button setTitle:@"我已阅并签署以下协议" forState:UIControlStateNormal];
    [button setTitleColor:[YZMAppStyle colorForGrayText] forState:UIControlStateNormal];
    [button.titleLabel setFont:[YZMAppStyle smallSizeFont]];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, -6)];
    
    __weak __typeof(&*self) weakSelf = self;
    [button bk_addEventHandler:^(UIButton *sender) {
        
        sender.selected = !sender.selected;
        weakSelf.submitBT.enabled = sender.selected;
        
    } forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(label.mas_bottom).offset(20);
    }];
    
    NSArray *contents = @[@"《微策略点买人参与沪深A股交易合作涉及费用及资费标准》",@"《微策略投资人与点买人参与沪深A股交易合作协议》",@"《微策略服务协议》"];
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB) {
        contents = @[@"《微策略投资人与点买人参与盈利宝合作协议》",@"《微策略服务协议》"];
    }
    
    UIView *delegateView = [UIView new];
    [self.view addSubview:delegateView];
    [delegateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(32);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(button.mas_bottom).offset(4);
    }];
    
    UIView *lastView = nil;
    for (NSString *content in contents) {
        
        UIButton *subBT = [UIButton new];
        subBT.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [subBT setTitle:content forState:UIControlStateNormal];
        [subBT setTitleColor:[YZMAppStyle colorForBlueText] forState:UIControlStateNormal];
        [subBT.titleLabel setNumberOfLines:0];
        subBT.tag = [contents indexOfObject:content];
        [subBT.titleLabel setFont:[YZMAppStyle smallSizeFont]];
        [subBT addTarget:self action:@selector(delegatePressed:) forControlEvents:UIControlEventTouchUpInside];
        [delegateView addSubview:subBT];
        [subBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(delegateView);
            if (lastView) {
                
                make.top.equalTo(lastView.mas_bottom).offset(6);
            }else{
                make.top.equalTo(delegateView);
            }
        }];
        
        lastView = subBT;
    }
    
    [delegateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView);
    }];
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = [YZMAppStyle colorForLabelMark];
    [self.view addSubview:bottomView];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(-1);
    }];
    
    self.submitBT = [YZMAppStyle submitButton];
    self.submitBT.enabled = YES;
    [self.submitBT setTitle:@"确定" forState:UIControlStateNormal];
    [self.submitBT addTarget:self action:@selector(submitDelegateAgreeRequest:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:self.submitBT];
    [self.submitBT mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(bottomView).insets(UIEdgeInsetsMake(12, 15, 12, 15));
    }];
}

@end
